package com.demo2do.fms.admin.web.controller.data.cost;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.data.DriverSalaryService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.authority.Organization;
import com.demo2do.fms.entity.data.cost.DriverSalary;

/**
 * 司机工资单价管理
 * 
 * @author Chang Liu
 * 
 */
@Controller
@MenuSetting("menu-data")
@RequestMapping("/driver-salaries")
public class DriverSalaryController {
    
    @Autowired
    @Qualifier("driverSalaryService")
    private DriverSalaryService driverSalaryService;
    
    /**
     * list by orgIds and page
     * 
     * @param orgIds
     * @param page
     * @return
     */
    @MenuSetting("submenu-driver-salary")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(@Secure(property = "orgIds") List<Long> orgIds, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("data/cost/driver-salary/driver-salary-list");
        
        int totals = driverSalaryService.count(orgIds);
        if(totals > 0) {
            
            List<DriverSalary> truckDriverSalaries = driverSalaryService.list(orgIds, page.with(totals));
            modelAndView.addObject("truckDriverSalaries", truckDriverSalaries);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * display search panel
     * 
     * @param orgIds
     * @return
     */
    @MenuSetting("submenu-driver-salary")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@Secure(property = "orgIds") List<Long> orgIds){
    	
        ModelAndView modelAndView = new ModelAndView("data/cost/driver-salary/driver-salary-search");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
        
    }
    
    /**
     * do search
     * 
     * @param organizationIds
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-driver-salary")
    @RequestMapping(value = "", method = RequestMethod.GET, params= "filter=true")
    public ModelAndView onSearch(@RequestParam(value="organizationIds", required = false) List<Long> organizationIds,
                                 Page page, HttpServletRequest request) {
        
        ModelAndView modelAndView = new ModelAndView("data/cost/driver-salary/driver-salary-list");
        
        int totals = driverSalaryService.count(organizationIds); 
        if(totals > 0) {
            
            List<DriverSalary> result = driverSalaryService.list(organizationIds, page.with(totals));
            modelAndView.addObject("truckDriverSalaries", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 选择公司和司机人数 load出现司机的工资
     * 
     * @param orgId
     * @param driverCount
     * @return
     */
    @MenuSetting("submenu-driver-salary")
    @RequestMapping(value = "", method = RequestMethod.GET, params= "prepare" , produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onPrepare(@RequestParam(value = "orgId", required=false) Long orgId, 
                                              @RequestParam(value = "driverCount", required=false) Integer driverCount){
        
        HttpResult httpResult = new HttpResult();
        
        DriverSalary driverSalary = driverSalaryService.getDriverSalary(orgId, driverCount);
        
        httpResult.success().data("driverSalary" , driverSalary);
            
        return httpResult;
    }
    
    /**
     * display create panel
     * 
     * @param orgIds
     * @return
     */
    @MenuSetting("submenu-driver-salary")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(@Secure(property = "orgIds") List<Long> orgIds) {
        
        ModelAndView modelAndView = new ModelAndView("data/cost/driver-salary/driver-salary-create");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
    }
    
    /**
     * on create
     * 
     * @param login
     * @param driverSalary
     * @param organization
     * @param driverCount
     * @return
     */
    @MenuSetting("submenu-driver-salary")
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(@Secure Login login,
                                             DriverSalary driverSalary,
                                             @Preloading("organization") Organization organization, 
                                             @RequestParam(value = "driverCount", required=false) Integer driverCount ){
        
        HttpResult httpResult = new HttpResult();
        
        driverSalaryService.create(login.getId(), organization, driverCount, driverSalary);
        
        httpResult.success().message("设置成功");
        
        return httpResult;
    }
    
    /**
     * 输出当前页记录
     * 
     * @param organizationIds
     * @param orgIds
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(@RequestParam(value="organizationIds", required = false) List<Long> organizationIds,
                               @Secure(property = "orgIds") List<Long> orgIds,
                               Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/cost/dirver-salary-export.xlsx"));

        // 1. list driverSalary
        List<Long> companyIds = CollectionUtils.isNotEmpty(organizationIds) ? organizationIds : orgIds;
        int totals = driverSalaryService.count(companyIds); 
        if(totals > 0) {
            List<DriverSalary> result = driverSalaryService.list(companyIds, page.with(totals));
            modelAndView.addObject("truckDriverSalaries", result);
        }
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}